import * as actionType from './constant'
import {PER_PAGE_NUMBER} from './constant'
import {handleSongsCategory} from 'utils/format-utils'
import {getCatList,getSongCategoryList} from 'services/song'
const changeCatagoryAction = res => ({
  type : actionType.CHANGE_CATAGORY,
  catagory : res
})

export const changeIsShowCatagory = boolean => ({
  type : actionType.CHANGE_IS_SHOW_CATAGORY, 
  isShowCataroty : boolean
})

const changeCatagorySongs = res => ({
  type : actionType.CHANGE_CATAGORY_SONGS,
  catagorySongs : res
})

export const changeCurrentCatagory = currentCatagory =>({
  type : actionType.CHANGE_CURRENT_CATAGORY,
  currentCatagory
})

export const getCatagoryAction = () => {
  return dispatch => {
    getCatList().then(res=>{
      const data = handleSongsCategory(res);
      dispatch(changeCatagoryAction(data));
    })
  }
}

export const getCatagorySongsAction = page => {
  return (dispatch,getState) => {
    const cat = getState().getIn(["songs","currentCatagory"]);
    getSongCategoryList(cat,page * PER_PAGE_NUMBER).then(res=>{
      dispatch(changeCatagorySongs(res));
    })
  }
}

